import request from '@/utils/request'
const appCode = 'SAD-APP'
/**
 * JSON对象转formData
 * @param {Object} JSON对象
  */
export function json2FormData(params) {
  const formData = new FormData()
  Object.keys(params).forEach((key) => {
    formData.append(key, params[key])
  })
  return formData
}

// /app/v1/helpCenterPage/
// 获取帮助中心页面
export function getHelpCenterPage(designId) {
  return request({
    method: 'GET',
    url: `/app/v1/helpCenterPage/`
  })
}
// /app/v1/helpCenterStorey/getStoreysByPageId/{pageId}
// 根据id获取页面详情
export function getStoreysByPageId(pageId) {
  return request({
    method: 'GET',
    url: `/app/v1/helpCenterStorey/getStoreysByPageId/${pageId}`
  })
}

// 根据分类id获取分页内容
export function getContentList({ page, rows, categoryId, sort, title, contentIds, classifyCode }) {
  const filtersRaw = []
  if (categoryId) {
    filtersRaw.push({ id: 'categoryId', property: 'categoryId', operator: '=', value: categoryId })
  }
  if (title) {
    filtersRaw.push({ id: 'title', property: 'title', operator: 'like', value: title })
  }
  if (contentIds && contentIds.length) {
    filtersRaw.push({ id: 'id', property: 'id', operator: 'in', value: `[${contentIds}]` })
  }
  const paramsRaw = {}
  if (classifyCode) {
    paramsRaw.classifyCode = classifyCode
  }
  if (contentIds && contentIds.length) {
    // 内容ID不为空时，查询所有模块
    paramsRaw.classifyCode = 'ALL'
  }
  const params = {
    page,
    rows,
    // sort: sort || 'lastUpdateTime',
    // order: 'desc',
    pageFlag: true,
    onlyCountFlag: false,
    filtersRaw: JSON.stringify(filtersRaw),
    sortersRaw: '[]',
    paramsRaw: JSON.stringify(paramsRaw)
  }
  if (sort) {
    params.sort = sort
    params.order = 'desc'
  }
  const data = json2FormData(params)
  return request({
    method: 'post',
    url: `/app/v1/helpCenterContent/pageContent`,
    data
  })
}
// 获取销售宝下树状类目
export function getCategoryTree() {
  return request({
    method: 'get',
    url: `/app/v1/helpCenterContent/queryCategoryTree`
  })
}
// 获取内容详情
export function getContentDetail(contentId) {
  return request({
    method: 'post',
    url: `/app/v1/helpCenterContent/browseContent`,
    data: { contentId, sourceCode: appCode }
  })
}
// 获取指定内容的上一页下一页
export function getUpDownContents({ contentId, categoryId, title, sort }) {
  const filtersRaw = []
  if (categoryId) {
    filtersRaw.push({ id: 'categoryId', property: 'categoryId', operator: '=', value: categoryId })
  }
  if (title) {
    filtersRaw.push({ id: 'title', property: 'title', operator: 'like', value: title })
  }
  const params = {
    page: 1,
    rows: 2,
    sort: sort || 'lastUpdateTime',
    order: 'desc',
    pageFlag: true,
    onlyCountFlag: false,
    filtersRaw: JSON.stringify(filtersRaw),
    sortersRaw: '[]',
    paramsRaw: '{}'
  }
  const data = json2FormData(params)
  return request({
    method: 'post',
    url: `/app/v1/helpCenterContent/getUpDownContentsByContentId/${contentId}`,
    data
  })
}

// 收藏内容
export function collectContent(contentId) {
  return request({
    method: 'post',
    url: `/app/v1/helpCenterContent/collectContent`,
    data: { contentId, sourceCode: appCode }
  })
}
/**
 * 点赞/点踩
 * @param {Number} likeType 点赞 1，点踩 -1
 * @param {String} contentId 内容Id
*/
export function updateLikeType({ contentId, likeType }) {
  return request({
    method: 'put',
    url: `/app/v1/helpCenterContent/updateLikeType`,
    data: { contentId, likeType, sourceCode: appCode }
  })
}

/**
 * 获取水印文件
 * @param { String }  id 文件ID
*/
export function getWatermarkFile(id) {
  return request({
    method: 'get',
    url: `/app/v1/helpCenterContent/downloadAttachmentAndAddWatermark/${id}`,
    responseType: 'blob',
    headers: {
      'Content-Type': 'application/json'
    }
  })
}
